package com.FH.organization.repair_type;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/4/28 0028.
 */
@Stateless
public class RepairTypeDAOImpl extends AccessDB implements RepairTypeDAO {

    @Override
    public ResultDTO getRepairTypeInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(search);

            String sqlexit = "select count(1) from repair_type where 1=1";

            String sql_w = "";//查询sql组合
            String sql_page;//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size;
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size;
            }
            if (json.has("repair_typeid")) {
                if(null != json.get("repair_typeid") && !json.get("repair_typeid").toString().isEmpty()){
                    sql_w += " and repair_typeid = '" + json.getString("repair_typeid") + "'";
                }
            }
            if (json.has("type")) {
                if(null != json.get("type") && !json.get("type").toString().isEmpty()){
                    sql_w += " and type = '" + json.getString("type") + "'";
                }
            }
            if (json.has("parent_id")) {
                if (json.getString("parent_id").equals("")) {
                    sql_w += " AND (parent_id IS NULL)";
                } else {
                    sql_w += " and parent_id = '" + json.getString("parent_id") + "'";
                }
            }
            String sql_ok = sqlexit + sql_w;
            String existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();
            List<RepairTypeShowDTO> result = new ArrayList<RepairTypeShowDTO>();

            if (!existName.equals("0")) {
                String sql = "select repair_typeid,repair_type_name,type,type_desc,parent_id from repair_type where 1=1" + sql_w + sql_page;
                seresult = this.getReportingTo(sql);
                System.out.println(sql);

                for (int i = 0; i < seresult.size(); i++) {
                    RepairTypeShowDTO repairTypeShowDTO = new RepairTypeShowDTO();

                    repairTypeShowDTO.setRepair_typeid(seresult.get(i)[1]);
                    repairTypeShowDTO.setRepair_type_name(seresult.get(i)[2]);
                    repairTypeShowDTO.setType(Integer.parseInt(seresult.get(i)[3]));
                    repairTypeShowDTO.setType_desc(seresult.get(i)[4]);
                    repairTypeShowDTO.setParent_id(seresult.get(i)[5]);
                    result.add(repairTypeShowDTO);
                }
                if (result.size() != 0) {
                    resultDTO.setStatus("90000");
                    resultDTO.setDescritpion(warnSrc.get_success);
                } else {
                    resultDTO.setStatus("90001");
                    resultDTO.setDescritpion(warnSrc.get_fail);
                }
                resultDTO.setStr(result.size() + "");
                resultDTO.setValues(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultDTO;
    }

    @Override
    public ResultDTO addRepairTypeInfo(RepairTypePullDTO repairTypePullDTO) {

        ResultDTO resultDTO = new ResultDTO();

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        String insert = "insert into repair_type (repair_typeid,repair_type_name,type,type_desc,parent_id) " +
                "values ('" + uuid + "','" + repairTypePullDTO.getRepair_type_name() + "'," + repairTypePullDTO.getType() + ",'" +
                repairTypePullDTO.getType_desc() + "','" + repairTypePullDTO.getParent_id() + "')";
        int result = addReportingTo(insert);
        System.out.println(insert);
        if (result != 0) {
            resultDTO.setStr(uuid);
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }
}
